package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.PracticeTask;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.service.IPracticeTaskService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 备课练课任务 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/api/practiceTask")
public class PracticeTaskController {

    @Autowired
    IPracticeTaskService iPracticeTaskService;

    @Autowired
    IQuarterService iQuarterService;

    @GetMapping("/getPracticeTaskList")
    public Result getPracticeTaskList( @RequestParam(value = "yearPart",required = false) Integer yearPart,
                                       @RequestParam(value = "quarterNum",required = false) Integer quarterNum) {
        if (yearPart == null || quarterNum == null) {
            Quarter quarter = iQuarterService.getNowQuarter();
            yearPart = quarter.getYearPart();
            quarterNum = quarter.getNum();
        }
        LocalDate nowDate = LocalDate.now();
        PracticeTask practiceTaskNow = iPracticeTaskService.getPracticeTaskNow(yearPart,quarterNum,nowDate.toString());
        QueryWrapper<PracticeTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("quarter_num",quarterNum);
        queryWrapper.orderByAsc("lesson_num");
        List<PracticeTask> taskList = iPracticeTaskService.list(queryWrapper);
        if (practiceTaskNow != null){
            for (PracticeTask practiceTask : taskList) {
                if (practiceTask.getLessonNum().equals(practiceTaskNow.getLessonNum())){
                    practiceTask.setIsNow(1);
                }
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("taskList",taskList);
        map.put("nowTask",practiceTaskNow);
        return new Result(Code.OK,map,"");

    }

}
